package service;

import constant.UserType;
import exception.InvalidPasswordException;
import exception.UserNotFoundException;

import java.sql.*;

import static util.DBUtil.conn;

// 用户服务类
public class UserService {
//    private static List<User> users = new ArrayList<>();
    public static Statement stmt;
    static {
        try {
            stmt = conn.createStatement();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
//        // 初始化一些测试用户
//        users.add(new User("admin", "admin123", UserType.ADMIN, true));
//        users.add(new User("manager", "manager123", UserType.MANAGER, true));
//        users.add(new User("receptionist", "receptionist123", UserType.RECEPTIONIST, true));
    }



    public static String login(String username, String password) throws UserNotFoundException, InvalidPasswordException {
        try {
            PreparedStatement psmt=conn.prepareStatement("SELECT 1 FROM users WHERE username=?");
            psmt.setString(1,username);
            if(psmt.executeQuery().next()){
                PreparedStatement p=conn.prepareStatement("SELECT password FROM users WHERE username=?");
                p.setString(1,username);
                ResultSet rs=p.executeQuery();
                if(rs.next()&&rs.getString("password").equals(password)){
                    return username;
                }
                else {
                    throw new InvalidPasswordException("密码错误");
                }
            }
            else{
                throw new UserNotFoundException("用户不存在");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void changePassword(String username, String oldPassword, String newPassword) throws InvalidPasswordException {
        try {
            if(isMatch(oldPassword,username)) {
                PreparedStatement psmt = conn.prepareStatement("UPDATE users SET password=? WHERE username=?");
                psmt.setString(1, newPassword);
                psmt.setString(2, username);
                psmt.executeUpdate();
            }
            else{
                throw new InvalidPasswordException("旧密码不正确");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    private static boolean isMatch(String old,String uname){
        try {
            PreparedStatement psmt=conn.prepareStatement("SELECT password FROM users WHERE username=?");
            psmt.setString(1,uname);
            ResultSet rs=psmt.executeQuery();
            return rs.next()&&rs.getString("password").equals(old);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    public static void resetPassword(String username, String newPassword) throws UserNotFoundException {
        try(PreparedStatement psmt=conn.prepareStatement("UPDATE users SET password=? WHERE username=?");){
            psmt.setString(1,newPassword);
            psmt.setString(2,username);
            int row=psmt.executeUpdate();
            if(row==0){
                throw new UserNotFoundException("用户不存在");
            }
            LogService.logAction(username, "重置密码");
        } catch (SQLException e) {
            throw new RuntimeException("数据库更新失败",e);
        }
    }

    public static void addUser(String username, String password, UserType userType) {
        try {
            PreparedStatement pstmt = conn.prepareStatement("INSERT INTO users (username, password, user_type, need_change_password) VALUES (?, ?, ?, ?)");
            pstmt.setString(1, username);
            pstmt.setString(2, password);
            pstmt.setString(3, userType.toString());
            pstmt.setBoolean(4, true);
            pstmt.executeUpdate();
            LogService.logAction(username, "创建用户: " + username);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }//插入数据库
    }

    public static void deleteUser(String user_name,String username) throws UserNotFoundException {
        try {
            PreparedStatement pstmt=conn.prepareStatement("DELETE FROM users WHERE username=?");
            pstmt.setString(1,user_name);
            int row=pstmt.executeUpdate();
            if(row>0){
                System.out.println("删除用户：");
                LogService.logAction(username, "删除用户: " + user_name);
            }
            else{
                throw new UserNotFoundException("用户不存在");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        //删除数据库
    }

}

